const Long = require("long");
const CryptoJS = require('crypto-js');

let key = CryptoJS.enc.Utf8.parse("1234123412ABCDEF");
let iv = CryptoJS.enc.Utf8.parse("ABCDEF1234123412");

function aesDecryptOld(t) {
  let r = CryptoJS.enc.Hex.parse(t);
  let e = CryptoJS.enc.Base64.stringify(r)
  let p = CryptoJS.AES.decrypt(e, key, {
    iv: iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7
  });
  let o = p.toString(CryptoJS.enc.Utf8);
  return o.toString()
}

function aesEncryptOld(t) {
  let r = CryptoJS.enc.Utf8.parse(t)
  let e = CryptoJS.AES.encrypt(r, key, {
    iv: iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7
  });
  return e.ciphertext.toString().toUpperCase()
}


let newkey = CryptoJS.enc.Utf8.parse("97KasXdlTzk1ut6y");
let newiv = CryptoJS.enc.Utf8.parse("nCIBpWEcW3GJ7HyB");

function aesDecryptNew(t) {
  let r = CryptoJS.enc.Hex.parse(t);
  let e = CryptoJS.enc.Base64.stringify(r)
  let p = CryptoJS.AES.decrypt(e, newkey, {
    iv: newiv,
    mode: CryptoJS.mode.CFB,
    padding: CryptoJS.pad.NoPadding
  });
  let o = p.toString(CryptoJS.enc.Utf8);
  return o.toString()
}

function aesEncryptNew(t) {
  let r = CryptoJS.enc.Utf8.parse(t)
  let e = CryptoJS.AES.encrypt(r, newkey, {
    iv: newiv,
    mode: CryptoJS.mode.CFB,
    padding: CryptoJS.pad.NoPadding
  });
  return e.ciphertext.toString()
}

let phone = '15038390051';
let longPhone = Long.fromString(phone);

console.log(longPhone.toBytes());
console.log(longPhone.shiftLeft(24).toBytes());

console.log(CryptoJS.enc.Utf8.parse(phone));

console.log(CryptoJS.enc.Utf8.stringify({
    words: [825569331, 942881072, 808792320],
    sigBytes: 11
  }
));
console.log(aesEncryptNew(phone));

let bytesView = new Uint8Array([3, 128, 91, 159, 35]);
console.log(bytesView);
let str = new TextDecoder().decode(bytesView);
console.log(str);
console.log(aesEncryptNew(str));


function parse(hexStr) {
  // Shortcut
  var hexStrLength = hexStr.length;
  
  // Convert
  var words = [];
  for (var i = 0; i < hexStrLength; i += 2) {
    let index = i >>> 3
    console.log(index);
    words[index] |= parseInt(hexStr.substr(i, 2), 16) << (24 - (i % 8) * 4);
  }
  
  return words;
}
let num = '15038390051';
console.log(parse(num));

